package thitracnghiem;

import Utils.Date;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Odotube
 */
public class SinhVien {

    String maSV;
    String hoTenSV;
    Date ngaySinh;
    String lop;
    static Connection conn;
    static Statement stm;

    public SinhVien(String maSV, String hoTenSV, String ngaySinh, String lop) {
        this.maSV = maSV;
        this.hoTenSV = hoTenSV;
        this.ngaySinh = Date.parseDate(ngaySinh);
        this.lop = lop;
    }

    public static void Open() throws SQLException {
        conn = DriverManager.getConnection("jdbc:odbc:ttn");
        stm = conn.createStatement();
    }

    public static void Close() throws SQLException {
        stm.close();
        conn.close();
    }

    public void setMaSV(String maSV) {
        this.maSV = maSV;
    }

    public String getMaSV() {
        return this.maSV;
    }

    public void setHoTenSV(String hoTenSV) {
        this.hoTenSV = hoTenSV;
    }

    public String getHoTenSV() {
        return this.hoTenSV;
    }

    public void setNgaySinh(String ngaySinh) {
        this.ngaySinh = Date.parseDate(ngaySinh);
    }

    public String getNgaySinh() {
        return this.ngaySinh.toString();
    }

    public void setLop(String lop) {
        this.lop = lop;
    }

    public String getLop() {
        return this.lop;
    }

    public static SinhVien ktDangNhap(String maSV, String hoTenSV, String ngaySinh, String lop) throws SQLException {
        conn = DriverManager.getConnection("jdbc:odbc:ttn");
        SinhVien sv = new SinhVien(maSV, hoTenSV, ngaySinh, lop);
        String query = "select * from SinhVien where masv ='" + maSV + "'";
        if (conn != null) {
            stm = conn.createStatement();
            ResultSet rs = stm.executeQuery(query);
            if (rs.next()) {
                String returnedHoTenSV = rs.getString("hotensv");
                String returnedNgaySinh = rs.getString("ngaysinh");
                String returnedLop = rs.getString("lop");

                if (returnedHoTenSV.equalsIgnoreCase(hoTenSV) && returnedLop.equalsIgnoreCase(lop) && Date.parseDate(returnedNgaySinh).equals(Date.parseDate(ngaySinh))) {
                    return sv;
                }
            }
        }
        return null;
    }

    public static ArrayList<SinhVien> getDSTheoLop(String lop) throws SQLException {
        ArrayList<SinhVien> mList = new ArrayList<SinhVien>();
        String sql = "select * from SinhVien where lop='" + lop + "'";
        
        ResultSet rs = stm.executeQuery(sql);
        while (rs.next()) {
            mList.add(new SinhVien(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
        }
        return mList;
    }
}
